import axios from 'axios'
import { ElMessage } from 'element-plus'

const service = axios.create({
  baseURL: 'http://127.0.0.1:8008/api',  // 使用相对路径，让代理处理
  timeout: 30000,
  headers: {
    'Content-Type': 'application/json'
  },
  withCredentials: false  // 修改为 false，因为不需要携带 cookies
})

// 请求拦截器
service.interceptors.request.use(
  config => {
    const timestamp = Date.now()
    config.params = { ...config.params, _t: timestamp }
    return config
  },
  error => {
    console.log('Request Error:', error)
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    console.log('Response Error:', error)
    ElMessage.error(error.message || '请求失败')
    return Promise.reject(error)
  }
)

export default service 